#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QScreen>
#include<QRect>
#include <QtNetwork>
#include <QHostAddress>
#include <QNetworkInterface>
#include <udp.h>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

//    QScreen* screen = QGuiApplication::primaryScreen();
//    QRect rect2 = screen->geometry();



    //qDebug() << rect2.topLeft();
    //qDebug() << rect2.bottomRight();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::do_update_system_log(QString log)
{
    ui->plainTextEdit_sys_log->appendPlainText(log);
}

void MainWindow::on_pushButton_connect_clicked()
{
    QHostAddress addr(my_def_desktop_ip);
    heart_config.desktop_ip = static_cast<uint32_t>(addr.toIPv4Address());
    heart_config.desktop_ip = reversebytes_uint32t(heart_config.desktop_ip);

   // connect(my_PicReceiverThread, &PicReceiverThread::update_pic_log, this, &MainWindow::do_update_Piclog );

    m_heartbeat_thread = new MYHeartbeatThread(&heart_config);
    connect(m_heartbeat_thread, &MYHeartbeatThread::update_heartbeat_log,
            this, &MainWindow::do_update_system_log);

    connect(m_heartbeat_thread, &MYHeartbeatThread::update_heartstate,
            this, &MainWindow::do_update_heartbeat_state);
    m_heartbeat_thread->start();
}

void MainWindow::do_update_heartbeat_state(int state)
{
    if(state == 0) {
        qDebug() << "lost connectiong lock!!!!";
    }
}

void MainWindow::on_pushButton_2_clicked()
{
    qDebug() << "agains";
    udp_sender.set_target_port(7777);
    udp_sender.set_target_ip("127.0.0.1");
    if(!udp_sender.init()) {
        qDebug() << "fail";
    }
    else {
        qDebug() << "success";
    }
}

void MainWindow::on_pushButton_5_clicked()
{
    uint8_t strddd[] = "sdsd";
    udp_sender.send(strddd, sizeof (strddd));
}

void MainWindow::on_pushButton_clear_log_clicked()
{
    ui->plainTextEdit_sys_log->clear();
}

void MainWindow::set_fps(double delay, double fps)
{
    char info[1024];
    sprintf(info, "%.02lfFPS %.02lfms", fps, delay);
    ui->Video_tag->setText(QString(info));
}

void MainWindow::do_update_video_frame(QImage image)
{
    static int cnt = 0;
    ui->video1->setPixmap(QPixmap::fromImage(image));
    cnt += 1;
    if (cnt == 20)
    {
        auto now = std::chrono::system_clock::now();
        auto duration = now.time_since_epoch();
        long long timestamp = std::chrono::duration_cast<std::chrono::microseconds>(duration).count();
        long long delay = timestamp - video_last_timestamp;
        set_fps(delay / 20000.0, 20000000.0 / delay);
        video_last_timestamp = timestamp;
        cnt = 0;
    }
}

void MainWindow::on_pushButton_pic_clicked()
{
    static bool pic_is_running = 0;
    if (pic_is_running == 1) {
        //close()
        ui->pushButton_pic->setText("打开图传");
        pic_is_running = 0;
    }
    else {
        //start()
        MyRecvVideoThread* m_recv_video_thread;
        m_recv_video_thread = new MyRecvVideoThread(my_def_video_port, 0);
        connect(m_recv_video_thread, &MyRecvVideoThread::update_video_frame,
        this, &MainWindow::do_update_video_frame);
        connect(m_recv_video_thread, &MyRecvVideoThread::update_video_log,
        this, &MainWindow::do_update_system_log);
        auto now = std::chrono::system_clock::now();
        auto duration = now.time_since_epoch();
        long long timestamp = std::chrono::duration_cast<std::chrono::microseconds>(duration).count();
        video_last_timestamp = timestamp;
        m_recv_video_thread->start();

        ui->pushButton_pic->setText("关闭图传");
        pic_is_running = 1;
    }

}

void MainWindow::on_checkBox_yg_lock_stateChanged(int arg1)
{
    if (arg1 == my_def_yg_lock)  //gou shang
    {

    }
    else //jie kai
    {

    }
}

void MainWindow::do_update_sbus_frame(std::vector<uint16_t> ch)
{
    ch[0] = ch[0];
    qDebug() << "update subs frame";
}
